package org.dromara.third.convert.riskObject.validator;

import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.riskObject.dto.RiskObjectDTO;
import org.dromara.third.convert.util.ExistenceValidator;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class RiskObjectValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return RiskObjectDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        RiskObjectDTO riskObjectDTO = (RiskObjectDTO) target;

        // 基础字段验证
        validateRequiredFields(riskObjectDTO, errors);

        // 条件验证
        validateConditionalFields(riskObjectDTO, errors);

        // 存在性验证
        validateExistence(riskObjectDTO, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(RiskObjectDTO dto, Errors errors) {
        ValidateUtil.rejectIfNull(dto::getId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主键不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getRiskObjectCode, String.valueOf(ExceptionEnum.AuthException01.getCode()), "风险分析对象编码不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getAnalysisName, String.valueOf(ExceptionEnum.AuthException01.getCode()), "风险分析对象名称不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getRiskObjectType, String.valueOf(ExceptionEnum.AuthException01.getCode()), "风险分析对象类型不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getIsDanger, String.valueOf(ExceptionEnum.AuthException01.getCode()), "是否重大危险源不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getHazardDept, String.valueOf(ExceptionEnum.AuthException01.getCode()), "部门负责人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getHazardLiablePerson, String.valueOf(ExceptionEnum.AuthException01.getCode()), "部门负责人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getAreaSite, String.valueOf(ExceptionEnum.AuthException01.getCode()), "地图面坐标不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateDept, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属部门不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
    }

    /**
     * 验证条件字段
     */
    private void validateConditionalFields(RiskObjectDTO dto, Errors errors) {
        // 如果是重大危险源，则必须填写危险源编码
        if ("1".equals(dto.getIsDanger())) {
            ValidateUtil.rejectIfEmpty(dto::getHazardCode, String.valueOf(ExceptionEnum.AuthException01.getCode()), "重大危险源编码不能为空", errors);
        }
    }

    /**
     * 验证存在性
     */
    private void validateExistence(RiskObjectDTO dto, Errors errors) {
        // 验证风险分析对象所属部门存在
        ExistenceValidator.validateDeptExists(dto.getHazardDept(), "风险分析对象所属部门", errors);

        // 验证部门负责人存在
        ExistenceValidator.validateUserExists(dto.getHazardLiablePerson(), "部门负责人", errors);

        // 验证所属部门存在
        ExistenceValidator.validateDeptExists(dto.getCreateDept(), "所属部门", errors);

        // 验证创建人存在
        ExistenceValidator.validateUserExists(dto.getCreateBy(), "创建人", errors);

        // 验证更新人存在
        ExistenceValidator.validateUserExists(dto.getUpdateBy(), "更新人", errors);
    }
}
